$(function() {
  $('.container-heigh').css({
    height: $(window).height()
  })
  $('.header').css({
    height: $(window).height()
  })
  var baseApi = 'http://api.esjlm.com/'
  var apiList = {
    banner: baseApi + '?a=getIndex&type=6',
    newsList: baseApi + '?a=getNewsList',
    formUrl: baseApi + '?a=addMemberJoin'
  }
  function swiperInit() {
    var mySwiper = new Swiper('.swiper-container', {
      autoplay: {
        disableOnInteraction: false,
        delay: 3500
      }, //可选选项，自动滑动
      loop: true,
      preventLinksPropagation: false,
      pagination: {
        el: '.swiper-pagination'
      }
    })
  }
  var utilAjax = function(obj, successFn) {
    $.ajax({
      url: obj.url,
      data: obj.data || {},
      dataType: obj.dataType || 'json',
      type: obj.type || 'get',
      success: function(data) {
        successFn(data.data)
      },
      error: function(err) {
        showtoastFromDiv(err)
      }
    })
  }

  function getBannerList() {
    var params = {
      url: apiList.banner,
      data: {}
    }
    utilAjax(params, function(data) {
      console.log(data)
      var bannerList = data.data.banner_list
      var htmlStr = ''
      $.each(bannerList, function(i, item) {
        var firstPageStr = ''
        var goUrl = item.url || 'javascript:;'
        htmlStr += `<div class="swiper-slide">
				     <a class="w" href="${goUrl}"><img class="w" src="${item.pic}" alt=""></a>
				 </div>`
      })
      $('.header .swiper-wrapper').html(htmlStr)
      var activityList = data.data.activity_list
      var result = []
      for (var i = 0; i < activityList.length; i += 2) {
        result.push(activityList.slice(i, i + 2))
      }
      var actStr = ''
      $.each(result, function(i, v) {
        var goUrl = v[0].url || 'javascript:;'
        var goUrls = v[1].url || 'javascript:;'
        actStr += `<div class="swiper-slide activit-box-slide">
                        <div class="activit-box-slide-in">
                        	<a class="w" href="${goUrl}"><img class="w" src="${v[0].pic}" alt=""></a>
                            <p>${v[0].title}</p>
                        </div>
                        <div class="activit-box-slide-in">
                        	<a class="w" href="${goUrls}"><img class="w" src="${v[1].pic}" alt=""></a>
                            <p>${v[1].title}</p>
                        </div>
                    </div>`
      })
      $('.activit-box .swiper-wrapper').html(actStr)
      swiperInit()
    })
  }
  getBannerList()

  function getNewsList() {
    var params = {
      url: apiList.newsList,
      data: {}
    }
    utilAjax(params, function(data) {
      var industry_news = data.data.industry_news
      var alliance_news = data.data.alliance_news
      var newsStr = ''
      var alliance = ''
      //
      var linka = `<a class="height-auto" href="${industry_news[0].n_url}"><img class="w" src="${industry_news[0].n_pic}" alt=""></a>`
      var linkb = `<a class="height-auto" href="${alliance_news[0].n_url}"><img class="w" src="${alliance_news[0].n_pic}" alt=""></a>`
      $.each(industry_news, function(i, item) {
        var goUrl
        if (item.n_url) {
          if (item.n_url.indexOf('?') > -1) {
            goUrl = item.n_url + '&' + 'id=' + item.n_id
          } else {
            goUrl = item.n_url
          }
        } else {
          goUrl = 'javascript:;'
        }
        if (data.data.industry_news.length > 0) {
          newsStr += `<li><a href="${goUrl}">${i + 1}. ${item.n_title}</a></li>`
          // newsStr += `<li><p>${i + 1}. ${item.n_title}</p></li>`
        }
      })
      $.each(alliance_news, function(i, item) {
        var goUrls
        if (item.n_url) {
          if (item.n_url.indexOf('?') > -1) {
            goUrls = item.n_url + '&' + 'id=' + item.n_id
          } else {
            goUrls = item.n_url
          }
        } else {
          goUrls = 'javascript:;'
        }
        if (data.data.alliance_news.length > 0) {
          // alliance += `<li><p>${i + 1}. ${item.n_title}</p></li>`
          alliance += `<li><a href="${goUrls}">${i + 1}. ${item.n_title}</a></li>`
        }
      })
      $('.industry-news').html(newsStr)
      $('.newsBox .alliance-agency').html(alliance)
      $('.industry-newsImg').html(linka)
      $('.alliance-agencyImg').html(linkb)
    })
  }
  getNewsList()

  // join submit
  function inputDialog(id) {
    var valid = ''
    switch (id) {
      case 'join-input-name':
        valid = '请填写姓名'
        break
      case 'join-input-tel':
        valid = '请填写电话'
        break
      case 'join-input-area':
        valid = '请填写地区'
        break
      case 'join-input-profession':
        valid = '请填写职业'
        break
    }
    return valid
  }
  function inputEmpty(id) {
    return $('#' + id).val()
  }
  function showtoastFromDiv(title) {
    $('.toast_div')
      .show()
      .text(title)
    setTimeout(function() {
      $('.toast_div').hide()
    }, 2000)
  }
  $('#join-submit').click(function() {
    if (!inputEmpty('join-input-name')) {
      showtoastFromDiv(inputDialog('join-input-name'))
      return false
    } else if (!inputEmpty('join-input-tel')) {
      showtoastFromDiv(inputDialog('join-input-tel'))
      return false
    } else if (!inputEmpty('join-input-area')) {
      showtoastFromDiv(inputDialog('join-input-area'))
      return false
    } else if (!inputEmpty('join-input-profession')) {
      showtoastFromDiv(inputDialog('join-input-profession'))
      return false
    }
    var formParams = {
      url: apiList.formUrl,
      type: 'post',
      data: $('#join-form').serialize()
    }
    utilAjax(formParams, function(res) {
      showtoastFromDiv(res.msg)
    })
  })

  //var showCode = $('.foot-showCode')
  //   $('.footer-supernatant-box > li').click(function(e) {
  //     $(this)
  //       .find('.foot-showCode')
  //       .parents('li')
  //       .siblings()
  //       .find('.foot-showCode')
  //       .hide()
  //     $(this)
  //       .find('.foot-showCode')
  //       .show()
  //     e.stopPropagation()
  //   })
  //   $(document).click(function(e) {
  //     showCode.hide()
  //     e.stopPropagation()
  //   })
  //   $(document).on('touchstart', function(e) {
  //     showCode.hide()
  //   })
  $('.footer-supernatant-box > li').click(function(e) {
    var currentClassname = e.currentTarget.className
    var imgUrl = ''
    switch (currentClassname) {
      case 'qrcode1':
        imgUrl = './assets/images/alliance-join-code.jpg'
        break
      case 'qrcode2':
        imgUrl = './assets/images/handle-code.jpg'
        break
      case 'qrcode3':
        imgUrl = './assets/images/agency-code.jpg'
        break
      case 'qrcode4':
        imgUrl = './assets/images/others-code.jpg'
        break
      default:
        break
    }
    showQrcode(imgUrl)
  })
  $('.qrCode').click(function() {
    $('.qrcode-img').hide()
    $(this).hide()
  })
  function showQrcode(src) {
    $('.qrCode').show()
    $('.qrcode-img').attr('src', src)
    $('.qrcode-img').show()
  }
})
